package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.mapper.SysOperLogMapper;
import com.ruoyi.system.service.ISysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 操作日志 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysOperLogServiceImpl implements ISysOperLogService {
    @Autowired
    private SysOperLogMapper operLogMapper;

    /**
     * 新增操作日志
     *
     * @param operLog 操作日志对象
     */
    @Override
    public void insertOperlog(SysOperLog operLog) {
        operLogMapper.insert(operLog);
    }

    /**
     * 分页查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public Page<SysOperLog> selectOperLogPage(SysOperLog operLog, Page<SysOperLog> page) {
        return getLambdaQueryChainWrapper(operLog).page(page);
    }

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog) {
        Map<String, String> params = operLog.getParams();
        return getLambdaQueryChainWrapper(operLog)
                .list();
    }

    private LambdaQueryChainWrapper<SysOperLog> getLambdaQueryChainWrapper(SysOperLog operLog) {
        Map<String, String> params = operLog.getParams();
        return new LambdaQueryChainWrapper<>(operLogMapper)
                .like(StringUtils.isNotEmpty(operLog.getOperIp()), SysOperLog::getOperIp, operLog.getOperIp())
                .like(StringUtils.isNotEmpty(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle())
                .eq(operLog.getBusinessType() != null, SysOperLog::getBusinessType, operLog.getBusinessType())
                .in(StringUtils.isNotEmpty(operLog.getBusinessTypes()), SysOperLog::getBusinessType, operLog.getBusinessTypes())
                .eq(operLog.getStatus() != null, SysOperLog::getStatus, operLog.getStatus())
                .like(StringUtils.isNotEmpty(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
                .eq(operLog.getOperName() != null, SysOperLog::getOperName, operLog.getOperName())
                .ge(params != null && StringUtils.isNotEmpty(params.get("beginTime")), SysOperLog::getOperTime, params == null ? "" : params.get("beginTime"))
                .le(params != null && StringUtils.isNotEmpty(params.get("endTime")), SysOperLog::getOperTime, params == null ? "" : params.get("endTime"))
                .orderByDesc(SysOperLog::getOperTime);
    }

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds) {
        return operLogMapper.deleteByIds(List.of(operIds));
    }

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId) {
        return operLogMapper.selectById(operId);
    }

}
